import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class HippieMobile implements Runnable {
	int n;
	int m;
	int[] next;
	int[] dest;
	int[] first;
	int[] enterTime;
	int[] component;
	boolean[] inStack;
	int[] lowlink;
	int[] stack;
	int sp;
	int componentId;
	int curTime;

    private void solve() {
		n = nextInt();
		m = nextInt();
		next = new int[m];
		dest = new int[m];
		first = new int[n];
		lowlink = new int[n];
		Arrays.fill(first, -1);
		for (int i = 0; i < m; ++i) {
			int a = nextInt() - 1;
			int b = nextInt() - 1;
			next[i] = first[a];
			dest[i] = b;
			first[a] = i;
		}
		enterTime = new int[n];
		Arrays.fill(enterTime, -1);
		inStack = new boolean[n];
		curTime = 0;
		componentId = 0;
		component = new int[n];
		stack = new int[n];
		sp = 0;
		Arrays.fill(component, -1);
		for (int i = 0; i < n; ++i) {
			if (enterTime[i] < 0) {
				dfs(i);
			}
		}
		int res = 0;
		for (int a = 0; a < n; ++a) {
			int edge = first[a];
			while (edge >= 0) {
				if (component[dest[edge]] != component[a]) {
					++res;
				}
				edge = next[edge];
			}
		}
		writer.println(res);
    }

	private int dfs(int at) {
		enterTime[at] = curTime++;
		int res = curTime - 1;
		inStack[at] = true;
		stack[sp++] = at;
		lowlink[at] = enterTime[at];
		int edge = first[at];
		while (edge >= 0) {
			int nxt = dest[edge];
			if (enterTime[nxt] < 0) {
				res = Math.min(res, dfs(nxt));
			} else {
				if (inStack[nxt]) {
					res = Math.min(res, lowlink[nxt]);
				}
			}
			edge = next[edge];
		}
		if (res == enterTime[at]) {
			while (true) {
				int cur = stack[--sp];
				component[cur] = componentId;
				inStack[cur] = false;
				if (cur == at) break;
			}
			++componentId;
		}
		lowlink[at] = res;
		return res;
	}

	public static void main(String[] args) {
        new HippieMobile().run();
    }

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter writer;

    static final String TASK_ID = "hippiemobile";

    public void run() {
        try {
            reader = new BufferedReader(new FileReader(TASK_ID + ".in"));
            tokenizer = null;
            writer = new PrintWriter(TASK_ID + ".out");
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    int nextInt() {
        return Integer.parseInt(nextToken());
    }

    long nextLong() {
        return Long.parseLong(nextToken());
    }

    double nextDouble() {
        return Double.parseDouble(nextToken());
    }

    String nextToken() {
        while (tokenizer == null || !tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(reader.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
}
